<?php

namespace App\Listeners\OpenShop;

use App\Events\OpenShop\JoinSucceededSmsSentEvent;
use App\Events\OpenShop\OrderCreatedEvent;
use App\Listeners\BaseListener;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderLogModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Support\ChangeSupport;
use GuzzleHttp\Exception\GuzzleException;

class JoinSucceededSmsLogListener extends BaseListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Handle the event.
     *
     * @param JoinSucceededSmsSentEvent $event
     * @return void
     */
    public function handle(JoinSucceededSmsSentEvent $event)
    {
        //获得订单ID
        $orderId = (int)OpenShopSignOrderModel::query()->where('orderNo', $event->order->orderNo)->value('id');
        if (!$orderId) {
            return;
        }
        $username = $event->userName;

//        $log               = new OpenShopSignOrderLogModel;
//        $log->orderId      = $orderId;
//        $log->content      = "加盟成功短信已发送";
//        $log->type         = OpenShopSignOrderLogModel::SEND_JOIN_SMS;
//        $log->operatorName = "商家：{$username}";
//        $log->createdAt    = strtotime($event->datetime);
//        $log->updatedAt    = strtotime($event->datetime);
//        $log->createdByUid = 0;
//        $log->updatedByUid = 0;
//        $log->changes      = "✉️加盟成功短信已发送";
//        $log->save();

        ChangeSupport::getInstance()->buildInsertChanges("✉️加盟成功短信已发送");
        ChangeSupport::getInstance()->batchChangesWriteToOrderLog(
            $orderId,
            OpenShopSignOrderLogModel::SEND_JOIN_SMS,
            "商家：{$username}"
        );
    }
}
